<?php $layout = 'app';
$title = '文章列表 - MyCMS'; ?>

<div style="margin-bottom: 2rem;">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
        <h1 style="margin: 0; color: #333;">文章列表</h1>
        <?php
        $currentUser = null;
        try {
            $currentUser = App\Services\Auth::user();
        } catch (Exception $e) {
            // 忽略错误
        }
        if ($currentUser): ?>
            <a href="/posts/create" class="btn">写文章</a>
        <?php endif; ?>
    </div>

    <!-- 搜索表单 -->
    <div style="background: white; padding: 1.5rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 2rem;">
        <form method="get" action="/posts" style="display: flex; gap: 1rem; align-items: center;">
            <input type="text" name="kw" value="<?= htmlspecialchars($_GET['kw'] ?? '') ?>"
                placeholder="搜索文章标题或内容..."
                style="flex: 1; padding: 0.75rem; border: 1px solid #ddd; border-radius: 4px; font-size: 1rem;">
            <button type="submit" class="btn">搜索</button>
            <?php if (!empty($_GET['kw'])): ?>
                <a href="/posts" class="btn btn-secondary">清除</a>
            <?php endif; ?>
        </form>
    </div>

    <!-- 文章列表 -->
    <?php if (empty($posts)): ?>
        <div style="background: white; padding: 3rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); text-align: center;">
            <h3 style="color: #666; margin-bottom: 1rem;">暂无文章</h3>
            <p style="color: #999; margin-bottom: 2rem;">
                <?php if (!empty($_GET['kw'])): ?>
                    没有找到包含 "<?= htmlspecialchars($_GET['kw']) ?>" 的文章
                <?php else: ?>
                    还没有发布任何文章
                <?php endif; ?>
            </p>
            <?php if ($currentUser): ?>
                <a href="/posts/create" class="btn">写第一篇文章</a>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div style="display: grid; gap: 1.5rem;">
            <?php foreach ($posts as $post): ?>
                <article style="background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                    <h2 style="margin: 0 0 1rem; font-size: 1.5rem;">
                        <a href="/posts/<?= $post['id'] ?>" style="color: #333; text-decoration: none;">
                            <?= htmlspecialchars($post['title']) ?>
                        </a>
                    </h2>

                    <div style="color: #666; font-size: 0.9rem; margin-bottom: 1rem;">
                        发布于 <?= date('Y-m-d H:i', strtotime($post['created_at'])) ?>
                        <?php if ($post['updated_at'] !== $post['created_at']): ?>
                            · 更新于 <?= date('Y-m-d H:i', strtotime($post['updated_at'])) ?>
                        <?php endif; ?>
                    </div>

                    <div style="color: #555; line-height: 1.6; margin-bottom: 1.5rem;">
                        <?= nl2br(htmlspecialchars(mb_substr($post['content'], 0, 200))) ?>
                        <?php if (mb_strlen($post['content']) > 200): ?>
                            <span style="color: #999;">...</span>
                        <?php endif; ?>
                    </div>

                    <div style="display: flex; gap: 1rem; align-items: center;">
                        <a href="/posts/<?= $post['id'] ?>" class="btn btn-secondary" style="font-size: 0.9rem;">
                            阅读全文
                        </a>

                        <?php if ($currentUser): ?>
                            <a href="/posts/<?= $post['id'] ?>/edit" style="color: #007bff; text-decoration: none; font-size: 0.9rem;">
                                编辑
                            </a>

                            <form method="post" action="/posts/<?= $post['id'] ?>/delete"
                                style="display: inline;"
                                onsubmit="return confirm('确定要删除这篇文章吗？')">
                                <?php echo App\Core\CSRF::field(); ?>
                                <button type="submit"
                                    style="background: none; border: none; color: #dc3545; cursor: pointer; font-size: 0.9rem; text-decoration: underline;">
                                    删除
                                </button>
                            </form>
                        <?php endif; ?>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>

        <!-- 分页导航 -->
        <?php if ($totalPages > 1): ?>
            <div style="margin-top: 2rem; text-align: center;">
                <div style="display: inline-flex; gap: 0.5rem; align-items: center;">
                    <?php
                    $currentPage = $page;
                    $baseUrl = '/posts';
                    $queryParams = [];
                    if (!empty($_GET['kw'])) {
                        $queryParams['kw'] = $_GET['kw'];
                    }

                    // 上一页
                    if ($currentPage > 1):
                        $prevParams = $queryParams;
                        $prevParams['page'] = $currentPage - 1;
                        $prevUrl = $baseUrl . '?' . http_build_query($prevParams);
                    ?>
                        <a href="<?= $prevUrl ?>" class="btn btn-secondary" style="font-size: 0.9rem;">上一页</a>
                    <?php endif; ?>

                    <?php
                    // 页码显示逻辑
                    $startPage = max(1, $currentPage - 2);
                    $endPage = min($totalPages, $currentPage + 2);

                    // 如果开始页码大于1，显示第一页和省略号
                    if ($startPage > 1):
                        $firstParams = $queryParams;
                        $firstParams['page'] = 1;
                        $firstUrl = $baseUrl . '?' . http_build_query($firstParams);
                    ?>
                        <a href="<?= $firstUrl ?>" class="btn btn-secondary" style="font-size: 0.9rem;">1</a>
                        <?php if ($startPage > 2): ?>
                            <span style="padding: 0.6rem; color: #666;">...</span>
                        <?php endif; ?>
                    <?php endif; ?>

                    <?php for ($i = $startPage; $i <= $endPage; $i++): ?>
                        <?php if ($i == $currentPage): ?>
                            <span class="btn" style="background: #007bff; color: white; font-size: 0.9rem;"><?= $i ?></span>
                        <?php else: ?>
                            <?php
                            $pageParams = $queryParams;
                            $pageParams['page'] = $i;
                            $pageUrl = $baseUrl . '?' . http_build_query($pageParams);
                            ?>
                            <a href="<?= $pageUrl ?>" class="btn btn-secondary" style="font-size: 0.9rem;"><?= $i ?></a>
                        <?php endif; ?>
                    <?php endfor; ?>

                    <?php
                    // 如果结束页码小于总页数，显示省略号和最后一页
                    if ($endPage < $totalPages):
                        if ($endPage < $totalPages - 1):
                    ?>
                            <span style="padding: 0.6rem; color: #666;">...</span>
                        <?php endif; ?>
                        <?php
                        $lastParams = $queryParams;
                        $lastParams['page'] = $totalPages;
                        $lastUrl = $baseUrl . '?' . http_build_query($lastParams);
                        ?>
                        <a href="<?= $lastUrl ?>" class="btn btn-secondary" style="font-size: 0.9rem;"><?= $totalPages ?></a>
                    <?php endif; ?>

                    <?php
                    // 下一页
                    if ($currentPage < $totalPages):
                        $nextParams = $queryParams;
                        $nextParams['page'] = $currentPage + 1;
                        $nextUrl = $baseUrl . '?' . http_build_query($nextParams);
                    ?>
                        <a href="<?= $nextUrl ?>" class="btn btn-secondary" style="font-size: 0.9rem;">下一页</a>
                    <?php endif; ?>
                </div>

                <div style="margin-top: 1rem; color: #666; font-size: 0.9rem;">
                    第 <?= $currentPage ?> 页，共 <?= $totalPages ?> 页
                    <?php if (!empty($_GET['kw'])): ?>
                        · 搜索结果
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>